package org.jeecg.modules.jxc.homepage.service.impl;


import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.jxc.baseData.entity.JxcGoodsBo;
import org.jeecg.modules.jxc.baseData.vo.VO;
import org.jeecg.modules.jxc.homepage.config.PageResult;
import org.jeecg.modules.jxc.homepage.entity.JxcGoods;
import org.jeecg.modules.jxc.homepage.mapper.JxcGoodsMapper;
import org.jeecg.modules.jxc.homepage.service.IJxcGoodsService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description: jxc_goods
 * @Author: jeecg-boot
 * @Date:   2021-12-20
 * @Version: V1.0
 */
@Service
public class JxcGoodsServiceImpl extends ServiceImpl<JxcGoodsMapper, JxcGoods> implements IJxcGoodsService {
    @Resource
    private JxcGoodsMapper jxcGoodsMapper;

    @Override
    public PageResult<JxcGoods> homePageList(Integer goodsTypeId, String goodsNameOrCode) {
        PageResult<JxcGoods> result = new PageResult<>();
        List<JxcGoods> list;
        try{
            list = jxcGoodsMapper.findListByTypeIdAndCode(goodsTypeId,Integer.valueOf(goodsNameOrCode));
        }catch (NumberFormatException e){
            list = jxcGoodsMapper.findListByTypeIdAndGoodsName(goodsTypeId,goodsNameOrCode);
        }
        result.setTotal(list.size());
        result.setRecords(list);
        return result;
    }

    @Override
    @Transactional(readOnly = true)
    public VO<JxcGoodsBo> queryGoodsList(String key, Integer pageNo, Integer pageSize) {
        Integer count = jxcGoodsMapper.selectGoodsCount(key);
        if (count == 0)
            return null;
        List<JxcGoodsBo> goodsBoList = jxcGoodsMapper.selectGoodsList(key,(pageNo-1)*pageSize,pageSize);
        VO<JxcGoodsBo> goodsBoVO = new VO<>();
        goodsBoVO.setPageNo(pageNo);
        goodsBoVO.setPageSize(pageSize);
        goodsBoVO.setData(goodsBoList);
        goodsBoVO.setTotal(count);
        return goodsBoVO;
    }

    @Override
    @Transactional(readOnly = true)
    public VO<JxcGoodsBo> queryNullGoodsList(String key, Integer pageNo, Integer pageSize) {
        Integer count = jxcGoodsMapper.selectNullGoodsCount(key);
        if (count == 0)
            return null;
        List<JxcGoodsBo> goodsBoList = jxcGoodsMapper.selectNullGoodsList(key,(pageNo-1)*pageSize,pageSize);
        VO<JxcGoodsBo> goodsBoVO = new VO<>();
        goodsBoVO.setPageNo(pageNo);
        goodsBoVO.setPageSize(pageSize);
        goodsBoVO.setData(goodsBoList);
        goodsBoVO.setTotal(count);
        return goodsBoVO;
    }

    @Override
    public void changeGoodsNumToNull(Integer id) {
        jxcGoodsMapper.updateGoodsNumToNull(id);
    }

    @Override
    public List<Map<String, Object>> queryGoods() {
        return jxcGoodsMapper.selectGoods();
    }

    /**
     * 库存报警
     */
    @Override
    public Map<String, JxcGoods> findStockLimit() {
        return jxcGoodsMapper.selectStockLimit();
    }

}
